package com.filldream.fastboot.base.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
//import com.baomidou.mybatisplus.MybatisConfiguration;
//import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
//import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
//import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * 多数据源配置
 * @author RickSun && iFillDream
 */
@Configuration
@MapperScan(basePackages = {"com.filldream.fastboot.base.**"}, sqlSessionFactoryRef = "sqlSessionFactory")
public class  DataSourceConfig {

	@Bean("dataSource")
	@ConfigurationProperties("project.db")
	public DataSource db1() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean("dataSource2")
	@ConfigurationProperties("project.db2")
	public DataSource db2() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean
	@Primary
	public DataSource multipleDataSource (@Qualifier("dataSource") DataSource db1
			,@Qualifier("dataSource2") DataSource db2 ) {
		DynamicDataSource dynamicDataSource = new DynamicDataSource();
		Map< Object, Object > targetDataSources = new HashMap<>();
		targetDataSources.put(DBTypeEnum.db1.getValue(), db1 );
		targetDataSources.put(DBTypeEnum.db2.getValue(), db2);
		dynamicDataSource.setTargetDataSources(targetDataSources);
		dynamicDataSource.setDefaultTargetDataSource(db1);
		return dynamicDataSource;
	}

	@Bean("sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
		sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2()));
		sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**/**Mapper.xml"));

		MybatisConfiguration configuration = new MybatisConfiguration();
		//configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
		configuration.setJdbcTypeForNull(JdbcType.NULL);
		configuration.setMapUnderscoreToCamelCase(true);
		configuration.setCacheEnabled(false);
		sqlSessionFactory.setConfiguration(configuration);
		sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
				mybatisPlusInterceptor() //添加分页功能
		});
		return sqlSessionFactory.getObject();
	}

//	@Bean
//	public PaginationInterceptor paginationInterceptor() {
//		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//		//paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持
//		return paginationInterceptor;
//	}

	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//		//添加乐观锁
//		mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		//添加分页
		mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
		return mybatisPlusInterceptor;
	}

//	@Bean
//	public MybatisPlusInterceptor performanceInterceptor() {
//		return new MybatisPlusInterceptor();
//	}



}
